/*
 * Copyright 2009 Cedric Priscal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android_serialport_api;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import android_serialport_api.bean.CalculatePersonTempBean;
import android_serialport_api.bean.ReadAllBean;
import android_serialport_api.bean.TempConfigBean;
import android_serialport_api.cardwriteread.R;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MainActivity extends Activity implements View.OnClickListener {

    private ReadWriteTempUtil mTempUtil;
    private TextView mShownResult;
    private EditText mInputRate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        EventBus.getDefault().register(this);//注册
        mTempUtil = ReadWriteTempUtil.getInstance();
        mShownResult = findViewById(R.id.tv);
        LinearLayout linearLayout = findViewById(R.id.btn_root);
        mInputRate = findViewById(R.id.et_rate);
        int btnCount = linearLayout.getChildCount();
        for (int i = 0; i < btnCount; i++) {
            if (linearLayout.getChildAt(i) instanceof Button) {
                linearLayout.getChildAt(i).setOnClickListener(this);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_read_config: {
                try {
                    mTempUtil.queryConfig();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case R.id.btn_calculate_person: {
                try {
                    mTempUtil.calculatePersonTemp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case R.id.btn_read_all: {
                try {
                    mTempUtil.readAll();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case R.id.btn_set_rate: {
                int newRate = 0;
                String readStr = mInputRate.getText().toString().trim();
                try {
                    newRate = Integer.parseInt(readStr);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "输入的发射率不是数字", Toast.LENGTH_LONG).show();
                    return;
                }
                if (newRate < 900 || newRate > 1100) {
                    Toast.makeText(this, "发射率必须在900-1100之间", Toast.LENGTH_LONG).show();
                    return;
                }
                try {
                    mTempUtil.setSendRate(newRate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            default:
                break;


        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEventBus(TempData eventData) {
        switch (eventData.getType()) {
            case TempData.TYPE_READ_CONFIG: {
                TempConfigBean configBean = (TempConfigBean) eventData.getObject();
                mShownResult.setText(configBean.toString());
                break;
            }
            case TempData.TYPE_CALCULATE_PERSON: {
                CalculatePersonTempBean calculatePersonTempBean = (CalculatePersonTempBean) eventData.getObject();
                mShownResult.setText(calculatePersonTempBean.toString());
                break;
            }
            case TempData.TYPE_READ_ALL: {
                ReadAllBean readAllBean = (ReadAllBean) eventData.getObject();
                mShownResult.setText(readAllBean.toString());
                break;
            }
            case TempData.TYPE_SET_SEND_RATE: {
                long modelNO = (long) eventData.getObject();
                mShownResult.setText("" + modelNO);
            }
            default:
                break;
        }
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        mTempUtil.destroy();
        super.onDestroy();
    }
}
